//
//  CloudViewCell.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/19.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

protocol CloudViewCellDelegate: NSObjectProtocol {
    func cloudViewCell(didTappedReport cell: CloudViewCell)
    func cloudViewCell(didDeleteOrder cell: CloudViewCell)
    func cloundViewCell(didRefoundOrder cell: CloudViewCell)
}

extension CloudViewCellDelegate {
    func cloudViewCell(didDeleteOrder cell: CloudViewCell) {}
    func cloundViewCell(didRefoundOrder cell: CloudViewCell) {}
}

class CloudViewCell: UITableViewCell {
    
    weak var delegate: CloudViewCellDelegate?
    
    private var viewModel: CloudOrderViewModel.RowViewModel?

    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .background
        contentView.backgroundColor = .background
    }
    
    func configure(with vm: CloudOrderViewModel.RowViewModel) {
        viewModel = vm
        avatarView.kf.setImage(with: URL(string: vm.url),
                               placeholder: UIImage(named: "avatar_default"))
        nameLabel.text = vm.doctorName
        groupLabel.text = vm.departmentName
        statusLabel.text = vm.status.description(readyFlag: vm.readyFlag)
        statusLabel.textColor = vm.status.statusColor(readyFlag: vm.readyFlag)
        timeLabel.text = vm.scheduleStr
        tipLabel.isHidden = vm.status == .cancel || vm.status == .comment
        reportBtn.isHidden = vm.status != .notStart && vm.status != .processing
        deleteBtn.isHidden = vm.status != .waitPay
        
        btnWContraint.constant = vm.status == .notStart && !vm.canSignIn ? 108 : 66
        tipLabel.attributedText = vm.tipText
        switch vm.status {
        case .notStart:
            if !vm.canSignIn {
                reportBtn.isEnabled = false
                reportBtn.setTitle("届时请来报道", for: .disabled)
                reportBtn.setBackgroundImage(UIImage(color: .white), for: .disabled)
                reportBtn.layer.borderWidth = 1
                reportBtn.layer.borderColor = UIColor.textLight.cgColor
            } else {
                reportBtn.isEnabled = vm.readyFlag == 0
                reportBtn.setTitle("报道", for: .normal)
                reportBtn.setBackgroundImage(UIImage(color: .mainColor), for: .normal)
                reportBtn.setTitle("已报道", for: .disabled)
                reportBtn.setBackgroundImage(UIImage(color: .background), for: .disabled)
                reportBtn.layer.borderWidth = 0
                reportBtn.layer.borderColor = UIColor.clear.cgColor
            }
            refoundBtn.isHidden = vm.canRefund == 0
        default:
            refoundBtn.isHidden = true
            break
        }
    }
    
    @IBOutlet private weak var btnWContraint: NSLayoutConstraint!
    
    @IBOutlet private weak var mainView: UIView! {
        didSet {
            mainView.backgroundColor = .white
            mainView.layer.cornerRadius = 8
            mainView.layer.masksToBounds = true
        }
    }
    
    @IBOutlet private weak var avatarView: UIImageView! {
        didSet {
            avatarView.contentMode = .scaleAspectFill
            avatarView.layer.cornerRadius = avatarView.height * 0.5
            avatarView.layer.masksToBounds = true
        }
    }
    
    @IBOutlet private weak var nameLabel: UILabel!
    
    @IBOutlet private weak var groupLabel: UILabel! {
        didSet {
            groupLabel.textColor = .mainColor
            groupLabel.font = .regular_14
        }
    }
    
    @IBOutlet private weak var statusLabel: UILabel!
    
    @IBOutlet private weak var timeLabel: UILabel!
    
    @IBOutlet private weak var tipLabel: UILabel!
    
    @IBOutlet weak var reportBtn: UIButton! {
        didSet {
            reportBtn.setBackgroundImage(UIImage(color: .mainGray), for: .highlighted)
            reportBtn.layer.cornerRadius = reportBtn.height * 0.5
            reportBtn.layer.masksToBounds = true
            reportBtn.setTitleColor(.white, for: .normal)
            reportBtn.setTitleColor(.textLight, for: .disabled)
            reportBtn.setBackgroundImage(UIImage(color: .mainColor), for: .normal)
        }
    }
    
    @IBOutlet weak var deleteBtn: UIButton! {
        didSet {
            deleteBtn.eventInterval = 2.0
            deleteBtn.isHidden = true
            deleteBtn.setBackgroundImage(UIImage(color: .mainGray), for: .highlighted)
            deleteBtn.layer.cornerRadius = deleteBtn.height * 0.5
            deleteBtn.layer.masksToBounds = true
            deleteBtn.setTitleColor(.white, for: .normal)
            deleteBtn.setTitleColor(.textLight, for: .disabled)
            deleteBtn.setBackgroundImage(UIImage(color: .mainColor), for: .normal)
        }
    }
    
    @IBOutlet weak var refoundBtn: UIButton! {
        didSet {
            refoundBtn.eventInterval = 2.0
            refoundBtn.isHidden = true
            refoundBtn.setBackgroundImage(UIImage(color: .mainGray), for: .highlighted)
            refoundBtn.layer.cornerRadius = refoundBtn.height * 0.5
            refoundBtn.layer.masksToBounds = true
            refoundBtn.setTitleColor(.white, for: .normal)
            refoundBtn.setTitleColor(.textLight, for: .disabled)
            refoundBtn.setBackgroundImage(UIImage(color: .mainColor), for: .normal)
        }
    }
    
    @IBAction private func reportBtnClicked(_ sender: UIButton) {
        sender.isEnabled = false
        sender.setTitle("已签到", for: .disabled)
        sender.setBackgroundImage(UIImage(color: .background), for: .disabled)
        delegate?.cloudViewCell(didTappedReport: self)
    }
    
    @IBAction private func deleteBtnClicked(_ sender: UIButton) {
        delegate?.cloudViewCell(didDeleteOrder: self)
    }
    
    @IBAction private func refoundBtnClicked() {
        delegate?.cloundViewCell(didRefoundOrder: self)
    }
    
    func reset() {
        reportBtn.isEnabled = true
        reportBtn.setTitle("报到", for: .normal)
    }
}
